<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet"%>
<%@taglib prefix="theme" uri="http://liferay.com/tld/theme" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@page import="javax.portlet.PortletURL"%>
<%@page import="com.myBook.slayer.model.MyBook"%>
<%@page import="com.myBook.slayer.model.impl.MyBookImpl"%>
<%@page import="com.myBook.slayer.service.MyBookLocalServiceUtil"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>




<portlet:defineObjects/>
<theme:defineObjects/>

<style>
	.error
	{
   	 color: red;
	}
</style>

<script src="<%=request.getContextPath() %>/js/jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="<%=request.getContextPath() %>/js/jquery-ui-1.8.13.custom.min.js" type="text/javascript"></script>
<script src="<%=request.getContextPath() %>/js/jquery.validate.min.js" type="text/javascript"></script>
<script src="<%=request.getContextPath() %>/js/jquery.validate.wrapper.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript">
 
$(document).ready(function() {
	$("#fm").validate({
		rules : {
			bookTitle : {
				required : true,
				minlength: 2
			},
			author : {
				required : true,
				minlength: 2
			}
		},
		
		messages :{
        		bookTitle: {
                required: "BookTitle is required",
                minlength: "Title needs to be at least length 2"
            },
            author: {
                required: "Author is required",
                minlength: "Author needs to be at least length 2"
            },
        },
	});
});
   
</script>
</head>

<% 

	String rootPortletId = portletDisplay.getRootPortletId();

	PortletURL formAction = renderResponse.createActionURL();
	formAction.setParameter("action", "addBook");
	formAction.setParameter("rootPortletId", rootPortletId);
	
	PortletURL updateBookURL = renderResponse.createActionURL();
	updateBookURL.setParameter("action", "updateBook");
	updateBookURL.setParameter("rootPortletId", rootPortletId);
	
	PortletURL listURL = renderResponse.createRenderURL();
	listURL.setParameter("action", "list");
	
	String url = formAction.toString();
	
	String action = ParamUtil.getString(renderRequest, "action");	
	if ("showUpdateForm".equalsIgnoreCase(action)) {
		url = updateBookURL.toString();
	}
		
%>


<h3>Add New Book</h3>
<form:form commandName="book" name="fm1" id="fm" method="post" action="<%=url%>">
	<table cellpadding="4">
		
		<tr>
			<td><form:input path="bookId" id="bookId" type="hidden"  size="30" maxlength="80" /></td>
		</tr>
		<tr>
			<td>Book Title</td>
			<td><form:input path="bookTitle" id="bookTitle" size="30" maxlength="80" /></td>
		</tr>
		<tr>
			<td colspan="2"><form:errors path="bookTitle" cssClass="error" /></td>
		</tr>
		<tr>
			<td>Author</td>
			<td><form:input path="author" id="author" size="30" maxlength="80"  /></td>
		</tr>
		<tr>
			<td colspan="2"><form:errors path="author" /></td>
		</tr>
		<tr>
			<td><input type="submit" value="Save" ></td>
			<td><input type="button" value="Back" onclick="javascript:history.back()"/></td>
		</tr>
		<tr>
			<td><a href="<%=listURL.toString() %>">Show all Books</a></td>
		</tr>
	</table>
</form:form>


